package com.qd.panda.service.third.dc;

import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.httpclientbootstart.util.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.qd.panda.model.CommodityStatistics;
import com.qd.panda.service.third.dc.dto.OrderReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自建数据中台
 *
 * @author sjk
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DataCenterService {
    private static final String CREATE_ORDER_URL = "/api/v1/dcOrder/createOrder";
    private static final String COMMODITY_STATISTICS_URL = "/api/v1/dcOrder/syncCommodityStatistics";
    private final HttpUtil httpUtil;
    private final ObjectMapper objectMapper;
    @Value("${dc.url}")
    private String url;
    @Value("${dc.syncConfigCode}")
    private String syncConfigCode;

    @SneakyThrows
    public ApiResultWrapper<Object> createOrder(OrderReqDTO req) {
        String json = httpUtil.sendPost(url + CREATE_ORDER_URL, objectMapper.writeValueAsString(req));
        return objectMapper.readValue(json, new TypeReference<>() {
        });
    }

    @SneakyThrows
    public ApiResultWrapper<String> commodityStatistics(List<CommodityStatistics> list) {
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("syncConfigCode",syncConfigCode);
        objectNode.putPOJO("statisticsList",list);
        String json = httpUtil.sendPost(url + COMMODITY_STATISTICS_URL, objectNode.toString());
        return objectMapper.readValue(json, new TypeReference<>() {
        });
    }
}
